﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using MillerApp.DataContract.Common;
using MillerApp.DataContract.MillerDB;
using MillerApp.DataAccess.MillerDB;
using MillerApp.UserControls;
using MillerApp.Stock.Common;
using MillerApp.DataContract.Common.Entity;
using MillerApp.Business.Stock;

namespace MillerApp.Stock
{
    public partial class AcquireStockInfosForm : SubFormBase
    {
        public AcquireStockInfosForm()
        {
            InitializeComponent();
        }

        private void AcquireStockInfosForm_Load(object sender, EventArgs e)
        {
            this.lblDate.Text = System.DateTime.Now.ToString("yyyy年MM月dd日");
            this.lblDay.Text = CommonLib.Utility.CommonHelper.GetDayOfWeekChinese(System.DateTime.Now.DayOfWeek);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Thread oThread = null;
            button1.Enabled = false;
            try
            {
                if (oThread != null)
                {
                    oThread.Abort();
                }

                List<KeyValueEntry> secCodeList = new List<KeyValueEntry>();

                //1. 自选股
                //string userid = "lingyeer";
                //lingyeer
                //if (rdbtnUser.Checked)
                //{
                //    string conditions = "and userid='" + userid + "'";
                //    int recordCount = 0;
                //    List<UsersecurityEntity> list =
                //        UsersecurityManager.GetUsersecuritysByCond(conditions, string.Empty, 0, int.MaxValue, out recordCount);

                //    if (list == null || list.Count <= 0)
                //    {
                //        return;
                //    }
                //    foreach (var item in list)
                //    {
                //        secCodeList.Add(new KeyValueEntry(item.stockexchangecode, item.seccode));
                //    }
                //}
                //2.所有股票
                if (this.rdbtnAll.Checked)
                {
                    int recordCount = 0;
                    List<SecurityEntity> list = SecurityManager.GetSecuritysByCond(string.Empty, string.Empty, 0, int.MaxValue, out recordCount);
                    if (list == null || list.Count <= 0)
                    {
                        return;
                    }
                    foreach (var item in list)
                    {
                        secCodeList.Add(new KeyValueEntry(item.stockexchangecode, item.seccode));
                    }
                }
                //用户输入
                if (this.rdbtnInput.Checked)
                {
                    char[] splitChars = { ';', ',', '；', '，', '@' };
                    string[] inputs = this.textBox1.Text.Split(splitChars, StringSplitOptions.RemoveEmptyEntries);
                    foreach (var item in inputs)
                    {
                        secCodeList.Add(new KeyValueEntry("sz", item));
                        secCodeList.Add(new KeyValueEntry("sh", item));
                    }
                }
                SecurityService service = new SecurityService();
                service.ALLSecCodeList = secCodeList;
                ParameterizedThreadStart threadStart = new ParameterizedThreadStart(service.AutoGetSecuritiesHQXX);
                oThread = new Thread(threadStart);
                DelegateMessageHanldler method = new DelegateMessageHanldler(HanlderMsgMethod);
                oThread.Start(method);
            }
            catch (Exception ex)
            {
                MessageBox.Show("出现异常，详细信息如下;"+ex.StackTrace);
            }
            finally
            {
                Thread.Sleep(200);
                button1.Enabled = true;
            }
        }

        private object HanlderMsgMethod(Dictionary<string, object> args)
        {
            try
            {
                //增加这个就不会出现以下的异常了
                if (!this.IsHandleCreated)
                {
                    return null;
                }
                foreach (var item in args)
                {
                    DelegateMessageHanldler method = new DelegateMessageHanldler(HanlderMsgMethod2);
                    //窗口关闭的时候会提示以下异常：
                    //在创建窗口句柄之前，不能在控件上调用 Invoke 或 BeginInvoke
                    lblMsg.Invoke(method, args);
                }
            }
            catch { }
            return null;
        }

        private object HanlderMsgMethod2(Dictionary<string, object> args)
        {
            foreach (var item in args)
            {
                lblMsg.Text = item.Value.ToString();
                this.Text = item.Value.ToString();
                if (item.Key.Trim().ToLower() == "FINISHED".ToLower())
                {
                    MessageBox.Show(item.Value.ToString());
                }
            }
            return null;
        }
    }
}
